package com.zmkj.taobao.taobaouserservice.controller;



import com.zmkj.taobao.taobaouserservice.apiresponse.BaseResponse;
import com.zmkj.taobao.taobaouserservice.apiresponse.ResponseMsg;
import com.zmkj.taobao.taobaouserservice.apiresponse.ResponseStatus;
import com.zmkj.taobao.taobaouserservice.entity.User;
import com.zmkj.taobao.taobaouserservice.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wll
 * @since 2020-05-26
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    IUserService iUserService;
    @RequestMapping("/list")
    public BaseResponse list(){
        List<User>   userList=iUserService.list();
        BaseResponse  result=new BaseResponse();
        result.setStatus(ResponseStatus.RESPONSE_200);
        result.setMsg(ResponseMsg.RESPONSE_SUCCESS);
        result.setResult(userList);
        return   result;
    }

    //根据用户名查询用户
    @GetMapping("/queryUserByUserName/{userName}")
    public  BaseResponse  queryUserByUserName(@PathVariable("userName") String  userName){
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("user_name",userName);
        List<User>   userList=  iUserService.listByMap(queryMap);
        BaseResponse  result=new BaseResponse();
        result.setStatus(ResponseStatus.RESPONSE_200);
        result.setMsg(ResponseMsg.RESPONSE_SUCCESS);
        result.setResult(userList);
        return   result;
    }
//    //根据用户名和密码查询  路径传参
//    @GetMapping("/queryUserByNameAndPwd/{userName}/{password}")
//    public List<User> queryUserByNameAndPwd(@PathVariable("userName") String userName,@PathVariable("password") String password){
////        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
////        queryWrapper.eq("user_name",userName);
//        Map<String,Object> queryMap=new HashMap<>();
//        queryMap.put("user_name",userName);
//        queryMap.put("password",password);
//        List<User> userList=iUserService.listByMap(queryMap);
//        return userList;
//    }
    //根据用户名查询用户  传统get方式传端个参数
    @GetMapping("/queryUserByUserNameAndPwd")
    public BaseResponse   queryUserByUserNameAndPwd(@RequestParam("userName") String  userName, @RequestParam("password") String password){
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("user_name",userName);
        queryMap.put("password",password);
        List<User>   userList=  iUserService.listByMap(queryMap);
        BaseResponse  result=new BaseResponse();
        result.setStatus(ResponseStatus.RESPONSE_200);
        result.setMsg(ResponseMsg.RESPONSE_SUCCESS);
        result.setResult(userList);
        return   result;
    }
    //删除用户信息接口
    @DeleteMapping("/remove/{id}")
    public  BaseResponse   remove(@PathVariable("id") Integer id){
        Boolean  data=  iUserService.removeById(id);
        BaseResponse  result=new BaseResponse();
        result.setStatus(ResponseStatus.RESPONSE_200);
        result.setMsg(ResponseMsg.RESPONSE_SUCCESS);
        result.setData(data);
        return result;
    }

    @PostMapping("/addUser")
    public  BaseResponse   addUser(@RequestBody User  user){
        boolean  data=  iUserService.save(user);
        BaseResponse  result=new BaseResponse();
        result.setStatus(ResponseStatus.RESPONSE_200);
        result.setMsg(ResponseMsg.RESPONSE_SUCCESS);
        result.setData(data);
        return  result;
    }
    @PutMapping("/updateUser")
    public  BaseResponse   updateUser(@RequestBody User  user){
        boolean  data=  iUserService.updateById(user);
        BaseResponse  result=new BaseResponse();
        result.setStatus(ResponseStatus.RESPONSE_200);
        result.setMsg(ResponseMsg.RESPONSE_SUCCESS);
        result.setData(data);
        return  result;
    }


}

